package jgl.vajra.kingkong.widget.base;

import android.content.Context;
import android.util.AttributeSet;

import jgl.vajra.kingkong.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.kingkong.widget.databindingadapter.BaseDataBindingAdapter;
import jgl.vajra.kingkong.widget.databindingadapter.BaseDataBindingDecorator;
import jgl.vajra.kingkong.widget.interfaces.PageErrorRetryListener;
import jgl.vajra.kingkong.widget.interfaces.RefreshLoadMoreListener;
import jgl.vajra.kingkong.widget.interfaces.RefreshRecyclerLoadStatusListener;
import jgl.vajra.kingkong.widget.recyclerView.pagemanagestrategy.LoadingManageStrategy;
import jgl.vajra.kingkong.widget.recyclerView.pagemanagestrategy.PageManageBuilder;
import jgl.vajra.kingkong.widget.recyclerView.pagemanagestrategy.PageManagerStrategy;
import jgl.vajra.kingkong.widget.recyclerView.pagemanagestrategy.RecyclerFootPageManageStrategy;
import jgl.vajra.kingkong.widget.recyclerView.pagemanagestrategy.RecyclerPageManger;
import jgl.vajra.kingkong.widget.utils.CommonUtils;
import jgl.vajra.kingkong.widget.utils.UIUtils;
import jgl.android.recyclerviewrefresh.IHeaderView;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.List;

import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.disposables.Disposable;

/**
 * Created by Mloong on 2018/7/27.
 */

public abstract class BaseRefreshViewLayout<D extends ViewDataBinding> extends BaseCustomView<D> implements PageErrorRetryListener {
    protected int action;
    protected int COUNT = 20;
    protected int currentPage = 1;
    public static final int ACTION_FIRST_LOAD = 1;
    public static final int ACTION_REFRESH = 2;
    public static final int ACTION_LOAD_MORE = 3;
    public static final int ACTION_TOP_LOAD_MORE = 4;
    public static final int ACTION_SECOND_LOAD_MORE = 5;
    // 刷新页面 不显示loading 区别于下拉刷新
    public static final int ACTION_PAGE_REFRESH = 6;
    protected boolean isFirstIn;
    protected RefreshLoadMoreListener mRefreshLoadMoreListener;

    /**
     * @Describe 判断使用pageManager 或者使用 recyclerView的foot进行loading处理
     */
    protected PageManageBuilder mBuilder;

    public RecyclerPageManger getRecyclerPageManger() {
        return mRecyclerPageManger;
    }

    protected RecyclerPageManger mRecyclerPageManger;
    private LoadingManageStrategy mLoadingStrategy;
    protected RefreshRecyclerLoadStatusListener mRefreshRecyclerLoadStatusListener;
    private Disposable netDisposable;
    protected boolean isShowErrorToast;
    private RefreshLayout refreshLayout;

    public BaseRefreshViewLayout(Context context) {
        this(context, null);
    }

    public BaseRefreshViewLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void initView(Context context, AttributeSet attrs) {

        isFirstIn = true;
        isShowErrorToast = true;
    }

    @Override
    public void initData(Context context) {

    }

    @Override
    public void initEvent(Context context) {

    }

    protected abstract void onRefresh();

    protected abstract void onLoadMore();

    public RefreshLayout getRefreshLayout() {
        return refreshLayout;
    }

    public void setRefreshLayout(RefreshLayout refreshLayout) {
        this.refreshLayout = refreshLayout;

    }


    protected void finishFootLoadMore() {
        getRefreshLayout().finishLoadMore();

    }


    public void finishHeadRefresh() {
        if (getRefreshLayout() != null)
            getRefreshLayout().finishRefresh(true);
    }

    public void requestNetStart(Disposable d) {
        if (netDisposable != null) {
            netDisposable.dispose();
            netDisposable = null;
        }
        this.netDisposable = d;
        if (mRefreshRecyclerLoadStatusListener != null) {
            mRefreshRecyclerLoadStatusListener.onStart(action);
        }
    }

    public void requestNetError(String errorInfo, int errorCode) {

        if (mRefreshRecyclerLoadStatusListener != null) {
            mRefreshRecyclerLoadStatusListener.onError(errorInfo, errorCode, action);
        }
    }

    public void requestNetEmpty() {
        if (mRefreshRecyclerLoadStatusListener != null)
            mRefreshRecyclerLoadStatusListener.onEmpty(action);

    }


    protected void showPageError() {
        mRecyclerPageManger.showPageError();
    }

    public void showPageEmpty() {
        mRecyclerPageManger.showPageEmpty();
    }

    public void showPageContent() {
        mRecyclerPageManger.showContent();
    }

    public void initPageManager() {

        mBuilder.setRecyclerView(getRecyclerView());
        mBuilder.setAdapter(mBuilder.getAdapter());
        if (mRecyclerPageManger == null)
            mRecyclerPageManger = new RecyclerPageManger();
        // 策略模式, 使用不同的对象
        if (mLoadingStrategy == null) {
            if (mBuilder.getChoosePageManager()) {
                mLoadingStrategy = new PageManagerStrategy(mBuilder);
            } else {
                mLoadingStrategy = new RecyclerFootPageManageStrategy(mBuilder);
            }
        } else {
            mLoadingStrategy.setBuilder(mBuilder);
        }

        mRecyclerPageManger.setBuilder(mBuilder);
        mRecyclerPageManger.setPageManagerStrategy(mLoadingStrategy);

        mLoadingStrategy.setPageErrorRetryListener(this);
    }

    public void requestNetSuccess(List listInfoPojos) {


    }

    public void firstLoad() {

    }

    public void refresh() {

    }

    protected void createBuilder() {
        if (mBuilder == null) mBuilder = new PageManageBuilder(getContext());
    }

    public void setLoadingMsg(String loadingMsg) {
        createBuilder();
        mBuilder.setLoadingMsg(loadingMsg);
    }

    protected void setErrorMsg(String retryMsg) {
        createBuilder();
        mBuilder.setRetryMsg(retryMsg);
    }


    public void setEmptyMsg(String emptyMsg) {
        createBuilder();
        mBuilder.setEmptyMsg(emptyMsg);
    }

    public void setEmptyMsg(int emptyMsg) {
        createBuilder();
        mBuilder.setEmptyMsg(UIUtils.getString(emptyMsg));
    }

    public void setEmptyRes(int emptyRes) {
        createBuilder();
        createBuilder();
        mBuilder.setEmptyImageRes(emptyRes);
    }

    public boolean isFirstIn() {
        return isFirstIn;
    }

    public void setIsFristIn(boolean isFristIn) {
        this.isFirstIn = isFristIn;
    }

    public int getAction() {
        return action;
    }

    private void setIsLoaderMore(boolean isLoadMore) {
        createBuilder();
        mBuilder.setLoadMore(isLoadMore);
        if (getRefreshLayout() != null)
            getRefreshLayout().setEnableLoadMore(isLoadMore);
    }

    public void smoothScrollToPosition(int position) {
        getRecyclerView().smoothScrollToPosition(position);
    }

    public void scrollToPosition(int position) {
        getRecyclerView().scrollToPosition(position);
    }


    public void setOnScrollListener(RecyclerView.OnScrollListener onScrollListener) {
        getRecyclerView().addOnScrollListener(onScrollListener);
    }

    public void setRecyclerViewBackgroundRes(int res) {
        getRecyclerView().setBackgroundResource(res);
    }

    public void setRootViewBackgroundRes(int res) {
        if (getRefreshLayout() != null)

            getRefreshLayout().getLayout().setBackgroundResource(res);
    }

    public void setChangeDuration(int i) {
        getRecyclerView().getItemAnimator().setChangeDuration(i);
    }

    protected abstract RecyclerView getRecyclerView();

    public void setRefreshLoaderMore(boolean refreshEnable, boolean loadMoreEnable) {
        //mPullRecyclerView.setRefreshLoaderMore(refreshEnable, loadMoreEnable);
        setIsLoaderMore(loadMoreEnable);
        setIsRefresh(refreshEnable);
    }

    public void setIsShowEmptyStatus(boolean f) {
        createBuilder();
        mBuilder.setShowEmptyStatus(f);
    }

    public void setIsShowErrorStatus(boolean f) {
        createBuilder();
        mBuilder.setShowErrorStatus(f);
    }

    protected void setRefreshDataNotShowLoading(boolean b) {
        createBuilder();
        mBuilder.setRefreshDataNotShowLoading(b);
    }

    protected void setLoadMoreDataNotShowLoading(boolean b) {
        createBuilder();
        mBuilder.setRefreshDataNotShowLoading(b);
    }

    public void setIsRefresh(boolean isRefresh) {
        createBuilder();
        mBuilder.setRefresh(isRefresh);
        if (getRefreshLayout() != null)
            getRefreshLayout().setEnableRefresh(isRefresh);
    }

    public void setLoadMoreAdapter(BaseDataBindingAdapter adapter) {
        setAdapter(adapter, true, null);
    }

    public void setNotLoadMoreAdapter(BaseDataBindingAdapter adapter) {
        setAdapter(adapter, false, null);
    }


    public void setManagerAdapter(BaseDataBindingAdapter adapter) {
        setManagerAdapter(adapter, false, null);
    }

    public void setLoadManagerAdapter(BaseDataBindingAdapter adapter) {
        setManagerAdapter(adapter, true, null);
    }

    public void setLoadMoreAdapter(BaseDataBindingAdapter adapter, RecyclerView.LayoutManager manager) {
        setAdapter(adapter, true, manager);
    }

    public void setAdapter(BaseDataBindingAdapter adapter) {
        setAdapter(adapter, true, null);
    }

    public void setAdapter(BaseDataBindingAdapter adapter, RecyclerView.LayoutManager manager) {
        setAdapter(adapter, true, manager);
    }

    public void setManagerAdapter(BaseDataBindingAdapter adapter, RecyclerView.LayoutManager manager) {
        setManagerAdapter(adapter, false, manager);
    }

    public void setLoadManagerAdapter(BaseDataBindingAdapter adapter, RecyclerView.LayoutManager manager) {
        setManagerAdapter(adapter, true, manager);
    }

    private void setManagerAdapter(BaseDataBindingAdapter adapter, boolean f, RecyclerView.LayoutManager layoutManager) {
        createBuilder();
        mBuilder.setAdapter(adapter);
        setIsLoaderMore(f);
        if (layoutManager == null)
            layoutManager = new LinearLayoutManager(getContext());
        getRecyclerView().setLayoutManager(layoutManager);
        getRecyclerView().setAdapter(adapter);
        CommonUtils.setManagerConfig(layoutManager, getRecyclerView());
    }

    private void setAdapter(BaseDataBindingAdapter adapter, boolean f, RecyclerView.LayoutManager layoutManager) {
        createBuilder();
        mBuilder.setAdapter(adapter);
        if (layoutManager == null)
            layoutManager = new LinearLayoutManager(getContext());
        getRecyclerView().setLayoutManager(layoutManager);
        getRecyclerView().setAdapter(adapter);
        setIsLoaderMore(f);
    }

    public void setAdapterItemPresenter(BaseBindingItemPresenter presenter) {
        if (mBuilder.getAdapter() == null) {
            return;
        }
        if (presenter == null) {
            return;
        }
        mBuilder.getAdapter().setItemPresenter(presenter);
    }


    public void setAdapterItemDecorator(BaseDataBindingDecorator d) {
        if (mBuilder.getAdapter() == null) {
            return;
        }
        if (d == null) {
            return;
        }
        mBuilder.getAdapter().setItemDecorator(d);
    }

    public void setIsRefreshCallBack(boolean f) {
        createBuilder();
        mBuilder.setIsRefreshCallBack(f);
    }

    public void setItemAnimator(RecyclerView.ItemAnimator animator) {
        getRecyclerView().setItemAnimator(animator);
    }

    public void addItemDecoration(RecyclerView.ItemDecoration animator) {
        getRecyclerView().addItemDecoration(animator);
    }

    public void setIsLoadMoreCallBack(boolean f) {
        createBuilder();
        mBuilder.setIsLoadMoreCallBack(f);
    }

    protected void setIsLoaderMoreSecondPageIng(boolean isLoaderMoreSecondPageIng) {
        mBuilder.setIsLoaderMoreSecondPageIng(isLoaderMoreSecondPageIng);
    }

    protected void setIsLoaderMoreSecondPageError(boolean isLoaderMoreSecondPageError) {
        mBuilder.setLoaderMoreSecondPageError(isLoaderMoreSecondPageError);
    }

    protected void setIsLoaderMoreSecondPageSuccess(boolean isLoaderMoreSecondPageSuccess) {
        mBuilder.setIsLoaderMoreSecondPageSuccess(isLoaderMoreSecondPageSuccess);
    }

    protected void setIsLoaderMoreSecondPageEmpty(boolean isLoaderMoreSecondPageSuccess) {
        mBuilder.setIsLoaderMoreSecondPageEmpty(isLoaderMoreSecondPageSuccess);
    }


    public int getCurrentPage() {
        return currentPage;
    }

    public void setOnLoadMoreListener(RefreshLoadMoreListener _loadMoreListener) {
        this.mRefreshLoadMoreListener = _loadMoreListener;
    }

    @Override
    public void errorRetry() {
        firstLoad();
        if (mRefreshLoadMoreListener!=null) {
            mRefreshLoadMoreListener.onRetry();
        }

    }

    /**
     * intro  加载状态的listener
     */
    public void setRefreshRecyclerLoadStatusListener(RefreshRecyclerLoadStatusListener loadDataStatusListener) {
        this.mRefreshRecyclerLoadStatusListener = loadDataStatusListener;
    }

    public void create(PageManageBuilder builder) {
        this.mBuilder = builder;
    }

    /**
     * intro  创建 配置参数类build
     */
    public void create(BaseDataBindingAdapter adapter) {
        createBuilder();
        mBuilder.setAdapter(adapter);
    }

    protected void loadMoreSecondPage(boolean b) {
        createBuilder();
        mBuilder.setLoadMoreSecondPage(b);
    }

    /**
     * intro 选择pageManager 或者是 recycleView的 foot模式刷新
     */
    public void setIsShowPageManager(boolean isShowPageManager) {
        createBuilder();
        mBuilder.setChoosePageManager(isShowPageManager);
    }

    /**
     * intro 设置是否自动加载下一页
     */
    public void setIsAuthLoadMoreSecondPage(boolean isAuthLoadMoreSecondPage) {
        createBuilder();
        mBuilder.setIsAuthLoadMoreSecondPage(isAuthLoadMoreSecondPage);
    }

    public void setPageLayoutTop(boolean f) {
        createBuilder();
        mBuilder.setPageLayoutTop(f);
    }

    public void setShowNoNet(boolean f) {
        createBuilder();
        mBuilder.setShowNoNet(f);
    }

    public void setHeadRefreshView(IHeaderView headerView) {
       // if (getRefreshLayout() != null)
           // getRefreshLayout().setHeaderView(headerView);
    }

    public void setPageManagerBackgroundRes(int pageManagerBackgroundRes) {
        createBuilder();
        mBuilder.setPageManagerBackgroundRes(pageManagerBackgroundRes);
    }

    public void setIsDataObject(boolean f) {
        createBuilder();
        mBuilder.setIsDataObject(f);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (netDisposable != null)
            netDisposable.dispose();
        mBuilder = null;
        netDisposable = null;
        mRecyclerPageManger = null;
        mLoadingStrategy = null;
    }

    public PageManageBuilder getBuilder() {
        createBuilder();
        return mBuilder;
    }

    public void setIsShowErrorToast(boolean isShowErrorToast) {
        this.isShowErrorToast = isShowErrorToast;
    }
}
